package view;


import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import com.chad.library.adapter.base.BaseQuickAdapter;
import java.util.ArrayList;
import java.util.List;
import adapter.PopupTextBookAdapter;
import adapter.testpaper.PopupKnowledgePointAdapter;
import adapter.testpaper.PopupMultiChapterAadpter;
import adapter.testpaper.PopupVersionAndLoreAdapter;
import adapter.testpaper.VersionAndLoreBean;
import application.App;
import bean.ChapterBean;
import bean.LoreBean;
import bean.TextBookBean;
import bean.VersionBean;
import student.andlei.com.student.R;

public class FourLayerPopupWindow extends PopupWindow implements View.OnClickListener {


//    private LoreBean loreBean;
//    private VersionBean versionBean;
    private Activity context;
//    private TextBookBean textbookBean;
//    private ChapterBean chapterbean;
    private List<ChapterBean> chapterList;
    private ChapterBean saveChapterBean;

    private List<VersionBean> versionList;
    private PopupVersionAndLoreAdapter versionAndLoreAdapter;
    private PopupTextBookAdapter textBookAdapter;
    private PopupMultiChapterAadpter chapterAdapter;
    private List<TextBookBean> textBookList;
    private LinearLayout linearLayout02;
    private LinearLayout linearLayout03;
    private List<TextBookBean> showGradeList = new ArrayList<>();
    private PopupKnowledgePointAdapter knowledgePointAdapter;
    private LinearLayout linearLayout04;
    private List<LoreBean> loreList;
    private List<VersionAndLoreBean> showVersionList;
    private List<VersionAndLoreBean> showLoreList;
    private LinearLayout loreBtContainer;
    private int color;

    public FourLayerPopupWindow(Activity context, List<VersionBean> versionBean, List<TextBookBean> textBookBean, List<LoreBean> loreBean) {
        super(context);
        this.context = context;
        this.versionList = versionBean;
        this.textBookList = textBookBean;
        this.loreList = loreBean;
        initalize();
    }

    private void initalize() {
        View view = LayoutInflater.from(context).inflate(R.layout.popup_four_layer, null);

        setContentView(view);
        initView(view);
        initWindow();

    }

    private void initView(View view) {
        RecyclerView recyclerView01 = view.findViewById(R.id.rc_first_four_layer);

        linearLayout02 = view.findViewById(R.id.ll_second_four_layer);
        RecyclerView recyclerView02 = view.findViewById(R.id.rc_second_four_layer);

        linearLayout03 = view.findViewById(R.id.ll_third_four_layer);
        RecyclerView recyclerView03 = view.findViewById(R.id.rc_third_four_layer);

        linearLayout04 = view.findViewById(R.id.ll_fourth_four_layer);
        RecyclerView recyclerView04 = view.findViewById(R.id.rc_fourth_four_layer);

        loreBtContainer = view.findViewById(R.id.ll_lore_container_second_four_layer);
        Button btLoreOk = view.findViewById(R.id.bt_ok_second_four_layer);
        Button btLoreCancel = view.findViewById(R.id.bt_cancel_second_four_layer);

        Button btOk = view.findViewById(R.id.bt_ok_fourth_four_layer);
        Button btCancel = view.findViewById(R.id.bt_cancel_fourth_four_layer);

        btLoreOk.setOnClickListener(this);
        btLoreCancel.setOnClickListener(this);

        btOk.setOnClickListener(this);
        btCancel.setOnClickListener(this);

        LinearLayoutManager firstLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        LinearLayoutManager secondLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        LinearLayoutManager thirdLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        LinearLayoutManager fourthLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        recyclerView01.setLayoutManager(firstLayoutManager);
        recyclerView02.setLayoutManager(secondLayoutManager);
        recyclerView03.setLayoutManager(thirdLayoutManager);
        recyclerView04.setLayoutManager(fourthLayoutManager);


        //versionList = versionBean.getData();
       // textBookList = textbookBean.getData().getList();
       // loreList = loreBean.getData().getList();
        showVersionList = new ArrayList<>();
        for (VersionBean dataBean : versionList) {
            showVersionList.add(new VersionAndLoreBean(dataBean.version_id, dataBean.version_name));
        }
        showLoreList = new ArrayList<>();
        for (LoreBean dataBean : loreList) {
            showLoreList.add(new VersionAndLoreBean(dataBean.lore_content, dataBean.lore_id));
        }


        List<String> data = new ArrayList<>();
        data.add("教材章节");
        data.add("知识点");
        knowledgePointAdapter = new PopupKnowledgePointAdapter(R.layout.item_rc_first_layer_chapter_dialog, data);
        versionAndLoreAdapter = new PopupVersionAndLoreAdapter(R.layout.item_rc_last_layer_chapter_dialog, showVersionList);
        textBookAdapter = new PopupTextBookAdapter(R.layout.item_rc_first_layer_chapter_dialog, showGradeList, textBookList.get(0));
        chapterAdapter = new PopupMultiChapterAadpter(R.layout.item_rc_last_layer_chapter_dialog, null);

        knowledgePointAdapter.bindToRecyclerView(recyclerView01);
        versionAndLoreAdapter.bindToRecyclerView(recyclerView02);
        textBookAdapter.bindToRecyclerView(recyclerView03);
        chapterAdapter.bindToRecyclerView(recyclerView04);

        knowledgePointAdapter.setOnItemClickListener(onItemClickListener);
        versionAndLoreAdapter.setOnItemClickListener(onItemClickListener);
        textBookAdapter.setOnItemClickListener(onItemClickListener);
        chapterAdapter.setOnItemClickListener(onItemClickListener);

//        versionAndLoreAdapter.setSaveTag(versionBean, loreList);

        reappearanceView();

        color = App.getInstance().getResources().getColor(R.color.color_light_gray);


    }

    private void reappearanceView() {
//        versionBean.saveVersionId = -1;
//        textbookBean.saveTextBookId = -1;

    }

    public void notifyChapterData(List<ChapterBean> chapterbean) {
        chapterList = chapterbean;
        if (chapterAdapter != null) {
            chapterAdapter.setNewData(chapterList);
        }
    }

    private BaseQuickAdapter.OnItemClickListener onItemClickListener = new BaseQuickAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            if (adapter instanceof PopupKnowledgePointAdapter) {
                //loreBean.getSaveLoreListBeanList().clear();
                knowledgePointAdapter.setTag(position);
                knowledgePointAdapter.notifyDataSetChanged();
                switch (position) {
                    case 0://教材章节
                        loreBtContainer.setVisibility(View.GONE);
//                        versionBean.saveVersionId = -1;
                        linearLayout02.setBackgroundColor(Color.WHITE);
                        versionAndLoreAdapter.setNewData(showVersionList);
                        break;
                    case 1://知识点
                        loreBtContainer.setVisibility(View.VISIBLE);
                        linearLayout02.setBackgroundColor(color);
                        versionAndLoreAdapter.setNewData(showLoreList);

                        break;
                }
                if (linearLayout02.getVisibility() == View.GONE) {
                    linearLayout02.setVisibility(View.VISIBLE);
                }
                if (linearLayout03.getVisibility() == View.VISIBLE) {
                    linearLayout03.setVisibility(View.GONE);
                    linearLayout04.setVisibility(View.GONE);
                }
            } else if (adapter instanceof PopupVersionAndLoreAdapter) {
                VersionAndLoreBean versionAndLoreBean = ((PopupVersionAndLoreAdapter) adapter).getItem(position);
                if (versionAndLoreBean.getVersion_id() > 0) {
                    for(VersionAndLoreBean bean:showVersionList){
                        if(bean.getVersion_id() == versionAndLoreBean.getVersion_id()){
                            bean.isSelect_version_id = true;
                        }else {
                            bean.isSelect_version_id = false;
                        }
                    }
                    classifyTextBookList(position);
                    if (linearLayout03.getVisibility() == View.GONE) {
                        linearLayout03.setVisibility(View.VISIBLE);
                    }

                    if (linearLayout04.getVisibility() == View.VISIBLE) {
                        linearLayout04.setVisibility(View.GONE);
                    }
                } else if (versionAndLoreBean.getLore_id() > 0) {
                    for(LoreBean loreBean:loreList){
                        if(loreBean.lore_id == versionAndLoreBean.getLore_id()){
                            loreBean.isSelectId = !loreBean.isSelectId;
                        }
                    }
                    versionAndLoreBean.isSelect_Lore_id = !versionAndLoreBean.isSelect_Lore_id;
                }
                versionAndLoreAdapter.notifyItemChanged(position);
            } else if (adapter instanceof PopupTextBookAdapter) {
                if (saveChapterBean != null) {
                    List<ChapterBean> saveChaterIdList = chapterList;
                    saveChaterIdList.clear();
                }
                int textbook_id = showGradeList.get(position).textbook_id;
                for(TextBookBean bean:showGradeList){
                    if(bean.textbook_id == textbook_id){
                        bean.isSelect = true;
                    }else {
                        bean.isSelect = false;
                    }
                }

//                textbookBean.saveTextBookId = textbook_id;
                if (linearLayout04.getVisibility() == View.GONE) {
                    linearLayout04.setVisibility(View.VISIBLE);
                }
                if (onSelectChapterAndLoreIdListener != null) {
                    onSelectChapterAndLoreIdListener.onSelectTextBookId(textbook_id);
                }
                textBookAdapter.notifyDataSetChanged();

            } else if (adapter instanceof PopupMultiChapterAadpter) {
                ChapterBean dataBean = chapterList.get(position);
                int chapter_id = dataBean.getChapter_id();
                dataBean.isSelectId = !dataBean.isSelectId;
                chapterAdapter.notifyDataSetChanged();
            }
        }


        private void classifyTextBookList(int position) {
            showGradeList.clear();
            String version_name = versionList.get(position).version_name;
            for (TextBookBean listBean : textBookList) {
                if (TextUtils.equals(version_name, listBean.version_name)) {
                    showGradeList.add(listBean);
                }
            }

            textBookAdapter.notifyDataSetChanged();
            versionAndLoreAdapter.notifyDataSetChanged();

        }

    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_ok_fourth_four_layer:
                if (onSelectChapterAndLoreIdListener != null) {
                    onSelectChapterAndLoreIdListener.onSelectChapterId(chapterList);
                }
                dismiss();
                break;
            case R.id.bt_cancel_fourth_four_layer:
               // saveChapterBean.getSaveChaterDataBeanList().clear();
                dismiss();
                break;

            case R.id.bt_ok_second_four_layer:
                if (onSelectChapterAndLoreIdListener != null) {
                    onSelectChapterAndLoreIdListener.onSelectLoreId(loreList);
                }
                dismiss();
                break;
            case R.id.bt_cancel_second_four_layer:
                //loreBean.getSaveLoreListBeanList().clear();
                dismiss();
                break;
        }
    }

    public interface OnSelectChapterAndLoreIdListener {
        void onSelectTextBookId(int textBookId);

        void onSelectChapterId(List<ChapterBean> saveChapterDataBeanList);

        void onSelectLoreId(List<LoreBean> saveLoreDataBeanList);
    }

    private OnSelectChapterAndLoreIdListener onSelectChapterAndLoreIdListener;

    public void setOnSelectChapterAndLoreIdListener(OnSelectChapterAndLoreIdListener onSelectChapterAndLoreIdListener) {
        this.onSelectChapterAndLoreIdListener = onSelectChapterAndLoreIdListener;
    }


    private void initWindow() {
        DisplayMetrics d = context.getResources().getDisplayMetrics();
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 531, context.getResources().getDisplayMetrics());
        this.setHeight(height);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.update();
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//        this.setBackgroundDrawable(dw);
        backgroundAlpha((Activity) context, 0.8f);//0.0-1.0
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha((Activity) context, 1f);
            }
        });
    }

    //设置添加屏幕的背景透明度
    public void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }
}
